package com.service;

import com.qqcommon.Message;
import com.qqcommon.MessageType;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

public class ServerConnectClientThread extends Thread {
    private String userId;
    private Socket socket;
    private boolean loop = true;

    public ServerConnectClientThread(String userId, Socket socket) {
        this.userId = userId;
        this.socket = socket;
    }

    public Socket getSocket() {
        return socket;
    }

    @Override
    public void run() {
        while (loop) {
            try {
                ObjectInputStream objectInputStream = new ObjectInputStream(socket.getInputStream());
                Message message = (Message) objectInputStream.readObject();
                if (message.getMesType().equals(MessageType.MESSAGE_GET_ONLINE_FRIEND)) {
                    System.out.println("用户" + message.getSender() + "需要在线用户列表");
                    Message message1 = new Message();
                    message1.setMesType(MessageType.MESSAGE_RET_ONLINE_FRIEND);
                    message1.setGetter(message.getSender());
                    message1.setContent(ManageServerConnectClientThread.getOnlineFriendList());
                    ObjectOutputStream objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
                    objectOutputStream.writeObject(message1);
                } else if (message.getMesType().equals(MessageType.MESSAGE_COMM_MES)) {
                    ObjectOutputStream objectOutputStream = null;

                    if (ManageServerConnectClientThread.getHm().get(message.getGetter()) == null) {
                        QQServer.addOffLineMes(message);
                    } else {
                        objectOutputStream = new ObjectOutputStream(ManageServerConnectClientThread.
                                getServerConnectClientThread(message.getGetter()).getSocket().getOutputStream());
                        objectOutputStream.writeObject(message);
                    }

                } else if (message.getMesType().equals(MessageType.MESSAGE_CLIENT_EXIT)) {
                    loop = false;
                    System.out.println("用户" + message.getSender() + "已离线");
                } else if (message.getMesType().equals((MessageType.MESSAGE_ALL_MES))) {
                    for (ServerConnectClientThread o : ManageServerConnectClientThread.getHm().values()) {
                        if (!o.userId.equals(message.getSender())) {
                            ObjectOutputStream objectOutputStream = new ObjectOutputStream(o.getSocket().getOutputStream());
                            objectOutputStream.writeObject(message);
                        }
                    }
                } else if (message.getMesType().equals(MessageType.MESSAGE_FILE_MES)) {
                    if (ManageServerConnectClientThread.getHm().get(message.getGetter()) == null) {
                        QQServer.addOffLineMes(message);
                    } else {
                        ObjectOutputStream objectOutputStream = new ObjectOutputStream(ManageServerConnectClientThread.
                                getServerConnectClientThread(message.getGetter()).getSocket().getOutputStream());
                        objectOutputStream.writeObject(message);

                    }
                }
            } catch (Exception e) {
                System.out.println("用户强制退出");
                loop = false;
            }

        }
    }
}
